package com.itszt.manager.controller;

import com.itszt.common.EasyUIPageDatasBean;
import com.itszt.common.ItemPicUtil;
import com.itszt.manager.VO.EasyUIPicUploadBean;
import com.itszt.manager.VO.EasyUISaveItemBean;
import com.itszt.manager.service.GoodsService;
import com.itszt.pojo.TbItem;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Controller
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    @Resource
    private FastDFSUtil fastDFSUtil;
//    @Value("${nginx_server}")
//    private String nginxServer;

    //以后统一使用lombok的@Slf4j门面日志
//    private Logger logger = LoggerFactory.getLogger(GoodsController.class);


    @RequestMapping("/item/list")
    @ResponseBody
    public EasyUIPageDatasBean<TbItem> getAllGoods(@RequestParam(required = false,defaultValue = "1") String page,
                                                   @RequestParam(required = false,defaultValue = "20") String rows){

        EasyUIPageDatasBean<TbItem> pageGoods = goodsService.getPageGoods(NumberUtils.toInt(page), NumberUtils.toInt(rows));

        return pageGoods;
    }


//    @RequestMapping("item-list")
//    public String goToItemList(){
//
//        return "item-list";
//
//    }
    @RequestMapping("{path}")
    public String goToItemList(@PathVariable(name="path") String path){

        return path;

    }

    // {"error":0/1,"url":"xxxxxx","message":"xxxxxx"}
    @RequestMapping("/pic/upload")
    @ResponseBody
    public EasyUIPicUploadBean uploadPic(MultipartFile uploadFile){
        String[] split = uploadFile.getOriginalFilename().split("\\.");
        String extName=split[1];
        EasyUIPicUploadBean easyUIPicUploadBean=new EasyUIPicUploadBean();

        try {
            String path = fastDFSUtil.uploadFile(uploadFile.getBytes(), extName, null);
            String url= ItemPicUtil.genFullPath(path);

            easyUIPicUploadBean.setError(0);
            easyUIPicUploadBean.setMessage("上传成功！");
            easyUIPicUploadBean.setUrl(url);

//            logger.debug("url----->"+url);
            return easyUIPicUploadBean;
        } catch (Exception e) {
            e.printStackTrace();
        }

        easyUIPicUploadBean.setError(1);
        easyUIPicUploadBean.setMessage("网络异常！");

        return easyUIPicUploadBean;

    }


    @RequestMapping("/item/save")
    @ResponseBody
    public EasyUISaveItemBean saveItem(TbItem item, String desc){
        EasyUISaveItemBean easyUISaveItemBean=new EasyUISaveItemBean();
        item.setImage(ItemPicUtil.genFdfsPath(item.getImage()));
        try {
            goodsService.addGoods(item,desc );
            easyUISaveItemBean.setStatus(EasyUISaveItemBean.STATUS_OK);
            easyUISaveItemBean.setMessage("新增商品完毕！ "+item.getTitle());
        } catch (Exception e) {
            e.printStackTrace();
            easyUISaveItemBean.setStatus(EasyUISaveItemBean.STATUS_ERROR);
            easyUISaveItemBean.setMessage("????！ "+item.getTitle());
        }

        return easyUISaveItemBean;

    }



}
